/*
 *    Copyright 2008-2009 The Digester Tools Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.digestertools.json;

import java.io.InputStream;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.digestertools.json.menu.Item;
import com.digestertools.json.menu.Menu;

/**
 * 
 * @author Simone Tripodi
 * @author Tommaso Teofili
 * @version $Id: JsonDigesterTestCase.java 372 2009-12-17 11:39:36Z simone.tripodi $
 */
public final class JsonDigesterTestCase {

    private static InputStream open(final String jsonResource) {
        return JsonDigesterTestCase.class.getResourceAsStream(jsonResource);
    }

    @DataProvider
    public static Object[][] jsonResources() {
        // menu test
        JsonDigester menuDigester = new JsonDigester();
        menuDigester.addObjectCreate("$", Menu.class);
        menuDigester.addBeanPropertySetter("$/header");

        menuDigester.addObjectCreate("$/items", Item.class);
        menuDigester.addBeanPropertySetter("$/items/id");
        menuDigester.addBeanPropertySetter("$/items/label");
        menuDigester.addSetNext("$/items", "addItem");

        Menu menu = new Menu();
        menu.setHeader("SVG Viewer");

        Item item = new Item();
        item.setId("Open");
        menu.addItem(item);

        item = new Item();
        item.setId("OpenNew");
        item.setLabel("Open New");
        menu.addItem(item);

        item = new Item();
        item.setId("ZoomIn");
        item.setLabel("Zoom In");
        menu.addItem(item);

        item = new Item();
        item.setId("ZoomOut");
        item.setLabel("Zoom Out");
        menu.addItem(item);

        item = new Item();
        item.setId("OriginalView");
        item.setLabel("Original View");
        menu.addItem(item);

        item = new Item();
        item.setId("Quality");
        menu.addItem(item);

        item = new Item();
        item.setId("Pause");
        menu.addItem(item);

        item = new Item();
        item.setId("Mute");
        menu.addItem(item);

        item = new Item();
        item.setId("Find");
        item.setLabel("Find...");
        menu.addItem(item);

        item = new Item();
        item.setId("FindAgain");
        item.setLabel("Find Again");
        menu.addItem(item);

        item = new Item();
        item.setId("Copy");
        menu.addItem(item);

        item = new Item();
        item.setId("CopyAgain");
        item.setLabel("Copy Again");
        menu.addItem(item);

        item = new Item();
        item.setId("CopySVG");
        item.setLabel("Copy SVG");
        menu.addItem(item);

        item = new Item();
        item.setId("ViewSVG");
        item.setLabel("View SVG");
        menu.addItem(item);

        item = new Item();
        item.setId("ViewSource");
        item.setLabel("View Source");
        menu.addItem(item);

        item = new Item();
        item.setId("SaveAs");
        item.setLabel("Save As");
        menu.addItem(item);

        item = new Item();
        item.setId("Help");
        menu.addItem(item);

        item = new Item();
        item.setId("About");
        item.setLabel("About Adobe CVG Viewer...");
        menu.addItem(item);

        return new Object[][] {
                { menuDigester, open("menu/menu.js"), menu },
        };
    }

    @Test(
            dataProvider = "jsonResources",
            dataProviderClass = JsonDigesterTestCase.class
    )
    public void parseAndMap(JsonDigester digester, InputStream input, Object expected) throws Exception {
        assert expected.equals(digester.parse(input));
    }

}
